using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms
{
    /// <summary>
    /// Form for creating new messages
    /// </summary>
    public partial class AEUCEditMessageDefinition : Form
    {
        List<IAEUCSetting> SelectedControlSettings = null;
        AEUCTabbedParentControl prntAssociate;
        GenMSG editMsg;
        #region Collect specific settings message utility UI
        CheckedListBox cbSetSel = new CheckedListBox();
        Boolean cbSetSelShouldDisplay = false;
        #endregion

        /// <summary>
        /// Public constructor of the form
        /// </summary>
        public AEUCEditMessageDefinition(AEUCTabbedParentControl iprntAssociate, GenMSG iEditMsg)
        {
            InitializeComponent();
            prntAssociate = iprntAssociate;
            rbMsgAEUC.Checked = true;
            rbNoPayload.Checked = true;
            editMsg = iEditMsg;

            #region Collect specific settings message utility UI
            cbSetSel.Size = new Size(cbCollectSpecTab.Size.Width, pnlSpecValue.Location.Y + pnlSpecValue.Size.Height - cbCollectSpecTab.Location.Y);
            cbSetSel.Location = new Point(cbCollectSpecTab.Location.X, cbCollectSpecTab.Location.Y);
            cbSetSel.Enabled = true;
            cbSetSel.Visible = false;
            cbSetSelShouldDisplay = false;
            cbSetSel.Parent = btnSelectSpecSettings.Parent;
            cbSetSel.BringToFront();
            #endregion

            updateEditUIForMsg(iEditMsg);
        }

        void updateEditUIForMsg(GenMSG iEditMsg)
        {
            if(iEditMsg.msgDestination == prntAssociate.cmcDefinitios.MSG_DEST_AEUC)
            {
                rbMsgAEUC.Checked = true;
            }
            else if (iEditMsg.msgDestination == prntAssociate.cmcDefinitios.MSG_DEST_SPECMSG)
            {
                rdMsgSPECMSG.Checked = true;
            }
            else
            {
                MessageBox.Show("Error getting message destination. Setting to default.");
                rbMsgAEUC.Checked = true;
            }

            cbMessageId.SelectedItem = iEditMsg.msgId;
            cbMessageStatus.SelectedItem = iEditMsg.msgStatus;

            if (iEditMsg.UseScriptAssignedSettings)
            {
                rbScriptAssignedSettings.Checked = true;
            }
            else if (iEditMsg.UseSpecControls)
            {
                rbCollectSpecificSettings.Checked = true;
                btnSelectSpecSettings_Click(this, EventArgs.Empty);
                for (int i = 0; i < iEditMsg.AssociatedControlSettings.Count;i++ )
                    cbSetSel.SetItemCheckState(cbSetSel.Items.IndexOf(iEditMsg.AssociatedControlSettings[i]), CheckState.Checked);
            }
            else if (iEditMsg.UseSpecTabPage)
            {
                rbCollectTab.Checked = true;
                cbCollectSpecTab.SelectedItem = iEditMsg.AssociatedTabPage;
            }
            else if (iEditMsg.UseSpecVal)
            {
                rbUseVal.Checked = true;
                nudVal.Value = iEditMsg.SpecVal;
                nudValSize.Value = iEditMsg.SpecValSize;
            }
            else
            {
                rbNoPayload.Checked = true;
            }

            chbEndSetTrailer.Checked = iEditMsg.AppendSetEndTrailer;
        }

        private void rbCollectTab_CheckedChanged(object sender, EventArgs e)
        {
            cbCollectSpecTab.Enabled = rbCollectTab.Checked;
            chbEndSetTrailer.Enabled = rbCollectTab.Checked;
            if (rbCollectTab.Checked)
            {
                cbCollectSpecTab.Items.Clear();
                cbCollectSpecTab.DisplayMember = "Text";
                for (int i = 0; i < prntAssociate.TabPages.Count; i++)
                {
                    cbCollectSpecTab.Items.Add(prntAssociate.TabPages[i]);
                }
                if (cbCollectSpecTab.Items.Count > 0)
                    cbCollectSpecTab.SelectedIndex = 0;
            }
        }

        private void rbCollectSpecificSettings_CheckedChanged(object sender, EventArgs e)
        {
            btnSelectSpecSettings.Enabled = rbCollectSpecificSettings.Checked;
            //chbEndSetTrailer.Enabled = rbCollectSpecificSettings.Checked;
            if (rbCollectSpecificSettings.Checked && cbSetSelShouldDisplay)
                cbSetSel.Visible = true;
            else
                cbSetSel.Visible = false;
        }

        private void rbScriptAssignedSettings_CheckedChanged(object sender, EventArgs e)
        {
            chbEndSetTrailer.Enabled = rbScriptAssignedSettings.Checked;
        }

        private void rbUseVal_CheckedChanged(object sender, EventArgs e)
        {
            pnlSpecValue.Enabled = rbUseVal.Checked;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            GenMSG createdMSG = null;

            GenMSGId msgID = (GenMSGId)cbMessageId.SelectedItem;
            GenMSGStatus msgStat = (GenMSGStatus)cbMessageStatus.SelectedItem;

            GenMSGDestination msgDst = new GenMSGDestination();
            #region Get the selected message destination
            if (rbMsgAEUC.Checked)
            {
                msgDst = prntAssociate.cmcDefinitios.MSG_DEST_AEUC;
            }
            else if (rdMsgSPECMSG.Checked)
            {
                msgDst = prntAssociate.cmcDefinitios.MSG_DEST_SPECMSG;
            }
            #endregion

            if (rbScriptAssignedSettings.Checked)
            {
                createdMSG = new GenMSG(prntAssociate,msgID, msgStat, msgDst, true, chbEndSetTrailer.Checked);
            }
            else if (rbCollectSpecificSettings.Checked)
            {
                SelectedControlSettings = new List<IAEUCSetting>();
                for (int i = 0; i < cbSetSel.CheckedItems.Count; i++)
                {
                    SelectedControlSettings.Add((IAEUCSetting)cbSetSel.CheckedItems[i]);
                }
                createdMSG = new GenMSG(prntAssociate, msgID, msgStat, msgDst, prntAssociate.SelectedTab, SelectedControlSettings, chbEndSetTrailer.Checked);
            }
            else if (rbCollectTab.Checked)
            {
                createdMSG = new GenMSG(prntAssociate, msgID, msgStat, msgDst, prntAssociate.SelectedTab, chbEndSetTrailer.Checked);
            }
            else if (rbUseVal.Checked)
            {
                createdMSG = new GenMSG(prntAssociate, msgID, msgStat, msgDst, (int)nudVal.Value, (uint)nudValSize.Value, chbEndSetTrailer.Checked);
            }
            else if (rbNoPayload.Checked)
            {
                createdMSG = new GenMSG(prntAssociate, msgID, msgStat, msgDst, chbEndSetTrailer.Checked);
            }

            createdMSG.msgDisplayName = tbMSGDisplayName.Text;

            //Update the editted message definition.
            editMsg.GenMSGUpdate(createdMSG);
            
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void rbMsgAEUC_CheckedChanged(object sender, EventArgs e)
        {
            if (rbMsgAEUC.Checked)
            {
                cbMessageId.Items.Clear();
                cbMessageStatus.Items.Clear();

                for (int i =0; i<prntAssociate.cmcDefinitios.AEUC_MSG_ID.Count; i++)
                {
                    cbMessageId.Items.Add(prntAssociate.cmcDefinitios.AEUC_MSG_ID[i]);
                }
                for (int i =0; i<prntAssociate.cmcDefinitios.AEUC_MSG_STAT.Count; i++)
                {
                    cbMessageStatus.Items.Add(prntAssociate.cmcDefinitios.AEUC_MSG_STAT[i]);
                }
                if (cbMessageId.Items.Count > 0)
                    cbMessageId.SelectedIndex = 0;
                if (cbMessageStatus.Items.Count > 0)
                    cbMessageStatus.SelectedIndex = 0;
            }
        }

        private void rdMsgSPECMSG_CheckedChanged(object sender, EventArgs e)
        {
            if (rdMsgSPECMSG.Checked)
            {
                cbMessageId.Items.Clear();
                cbMessageStatus.Items.Clear();

                for (int i =0; i<prntAssociate.cmcDefinitios.SPECMSG_MSG_ID.Count; i++)
                {
                    cbMessageId.Items.Add(prntAssociate.cmcDefinitios.SPECMSG_MSG_ID[i]);
                }
                for (int i =0; i<prntAssociate.cmcDefinitios.SPECMSG_MSG_STAT.Count; i++)
                {
                    cbMessageStatus.Items.Add(prntAssociate.cmcDefinitios.SPECMSG_MSG_STAT[i]);
                }
                if (cbMessageId.Items.Count > 0)
                    cbMessageId.SelectedIndex = 0;
                if (cbMessageStatus.Items.Count > 0)
                    cbMessageStatus.SelectedIndex = 0;
            }
        }

        private void btnSelectSpecSettings_Click(object sender, EventArgs e)
        {
            cbSetSelShouldDisplay = true;
            cbSetSel.Visible = true;
            for (int i = 0; i < prntAssociate.SelectedTab.Controls.Count; i++)
            {
                if (prntAssociate.SelectedTab.Controls[i] is IAEUCSetting)
                {
                    cbSetSel.Items.Add(((IAEUCSetting)prntAssociate.SelectedTab.Controls[i]));
                }
            }
        }
    }
}